package controller;

import model.UserInfo;
import model.UserNameToId;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-12-06
 * Time: 22:48
 */
@WebServlet("/register")
public class Register extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("utf8");
        //从请求中拿到用户名和密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username + " " + password);
        resp.sendRedirect("microblog_list.html");
    }
    //    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
////        req.setCharacterEncoding("utf8");
////        //从请求中拿到用户名和密码
////        String username = req.getParameter("username");
////        String password = req.getParameter("password");
////        System.out.println(username);
////        //字符串哈希(将用户名转化成id)
////        UserNameToId userNameToId = new UserNameToId();
////        int userId = userNameToId.getId(username);
////        System.out.println(userId);
////        if(username == null || password == null || username.equals("") || password.equals("")){
////            resp.setContentType("text/html;charset=utf8");
////            System.out.println("输入的用户名或者密码不能为空");
////            resp.getWriter().write("输入的用户名或者密码不能为空");
////
////            return;
////        }
////        //判断这个账户是否已经存在
////        if(UserInfo.users[userId] != null){
////            resp.setContentType("text/html;charset=utf8");
////            resp.getWriter().write("无法注册: 此账户已经存在");
////            return;
////        }
////        System.out.println("添加成功");
////        UserInfo userInfo = new UserInfo();
////        userInfo.insert(userId, username, password);
//
//        //注册成功, 转跳到登录页面
//        resp.sendRedirect("login.html");
//    }
}
